package cn.buildoncloud.labor.workermanapplet.interceptor;

import cn.buildoncloud.labor.workermanapplet.common.base.Result;
import cn.buildoncloud.labor.workermanapplet.util.AppletUtil;
import com.alibaba.fastjson.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: cn.buildoncloud.labor.workermanapplet.interceptor
 * Created by CuiZibo on 2018/11/1 15:01
 */
@Component
public class AuthInterceptor implements HandlerInterceptor {

    private static Logger log = LoggerFactory.getLogger(AuthInterceptor.class);

    @Value("${webSystemIP}")
    private String webSystemIP;

    @Autowired
    private Dao dao;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        String openId = httpServletRequest.getHeader("openId");

        Result result = null;
        if (openId == null) {
            result = Result.error(-99, "没有访问权限,请重新登陆！");
            AppletUtil.returnJson(httpServletResponse, JSONObject.toJSONString(result));
            return false;
        } else {
            int i = dao.count("applet_user", Cnd.where("openid", "=", openId));
            if (i != 1) {
                result = Result.error(-98, "用户未绑定，请进行绑定！");
                AppletUtil.returnJson(httpServletResponse, JSONObject.toJSONString(result));
                return false;
            }
        }
        return true;

    }

    public static Map<String, String> getParameterMap(HttpServletRequest request) {
        // 参数Map
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, String> returnMap = new HashMap();
        parameterMap.forEach((k, v) -> {
            String value = "";
            if (null == v) {
                value = "";
            } else if (v instanceof String[]) {
                String[] values = (String[]) v;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = v.toString();
            }
            returnMap.put(k, value);
        });
        return returnMap;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
